package com.shishuo.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shishuo.cms.dao.ArticleDao;
import com.shishuo.cms.entity.ArticleEntity;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.util.CopyUtil;
import com.shishuo.cms.vo.ArticleEntityVo;

@Service
public class ArticleService extends BaseService<ArticleEntity, Long> {
	private ArticleDao articleDao;

	@Autowired
	public void setArticleDao(ArticleDao articleDao) {
		super.setBaseDao(articleDao);
		this.articleDao = articleDao;
	}
	@Autowired
	private ClassesService classesService;
	
	public PageVo<ArticleEntityVo> getAllListPage(int pageNum) throws Exception{
		PageVo<ArticleEntityVo> pageVo = new PageVo<ArticleEntityVo>(pageNum);
		/*List<ArticleEntityVo> list = new ArrayList<ArticleEntityVo>();
		List<ArticleEntity> old=this.getAllList(pageVo.getOffset(), pageVo.getRows());
		for (int i = 0; i < old.size(); i++) {
			ArticleEntityVo articleEntityVo=new ArticleEntityVo();
			CopyUtil.Copy(old.get(i), articleEntityVo);
			articleEntityVo.setClassesEntity(classesService.getById(old.get(i).getClassId()));
			list.add(articleEntityVo);
		}*/
		List<ArticleEntityVo> list =articleDao.getAllListVo(pageVo.getOffset(), pageVo.getRows(),0);
		pageVo.setList(list);
		pageVo.setCount(articleDao.getAllListVoCount(0));
		return pageVo;
	}
	/**
	 * 通过类型ID及其当前页获取文章列表
	 * @param page
	 * @param id
	 * @throws Exception 
	 */
	public PageVo<ArticleEntityVo> getAllListPageByClassId(int pageNum, long classId,String classAliasName) throws Exception {
		PageVo<ArticleEntityVo> pageVo = new PageVo<ArticleEntityVo>(pageNum);
		List<ArticleEntityVo> list =articleDao.getAllListVo(pageVo.getOffset(), pageVo.getRows(),classId);
		pageVo.setList(list);
		pageVo.setCount(articleDao.getAllListVoCount(classId));
		pageVo.setDirPath("/article/"+classAliasName+"/");
		return pageVo;
	}
	public ArticleEntityVo getVoById(long articleId) throws Exception{
		return articleDao.getVoById(articleId);
	}
	/**
	 *
	 * @param topCount
	 * @param aliasName
	 * @return
	 */
	public List<ArticleEntityVo> getTopArticle(int topCount, String aliasName)
	{
		// TODO Auto-generated method stub
		return articleDao.getTopVoByAliasName(topCount,aliasName);
	}
}
